Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
regex capture content of {} only if no part is inside "" (and other encapsulators)

are regular expressions even appropriate for this problem? I'm trying to extract blocks of text surrounded by 'raw' curly braces. so anything surrounded by "" or **,"",~~,``, should be ignored entirely. sample text:

{
"text1"
'text2'
*text3*
~text4~
`text5`
}
{
"half  broken1}{"
*half  broken2}{*
'half  broken3}{'
~half  broken4}{~
`half  broken5}{`
}
{
"{nested text1}"
'{nested text2}'
~{nested text3}~
*{nested text4}*
`{nested text5}`
}
{
"text1"
{
`text2`
}
}

expected output:

[
"\"text1\"
'text2'
*text3*
~text4~
`text5`",

"\"half  broken1}{\"
*half  broken2}{*
'half  broken3}{'
~half  broken4}{~
`half  broken5}{`",

"\"{nested text1}\"
'{nested text2}'
~{nested text3}~
*{nested text4}*
`{nested text5}`",

"\"text1\"
{
`text2`
}",
"`text2`"
]

I can target curly braces with:

/(?<=\{)[^}]*(?=\})/gm

but that eats the first } facing brace it finds to match. I have tried a few other strings, but so far - I don't know how to ignore {"}"} the first brace in this case.

(using js syntax for regex)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

https://regex101.com/r/EODDXL/1

(?<=\{)(?:"[^"]*"|'[^']*'|\*[^*]*\*|~[^~]*~|`[^`]*`|{[^}]*}|[^}])*?(?=\})

That's starting with what you had, but includes ignoring " followed by 0 or more [^"] followed by a ". Then repeat this pattern for ', *, ~, ` and {}. This captures the outer groups only. I'm not sure how exactly you'd get the expected output at the end, but this may get you in the right direction.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!